package top.xia17.plugins.fast.common.database.redis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * redis配置
 * @author xia17
 * @date 2021/2/4 17:02
 */
@Configuration
@ConditionalOnClass(RedisConnectionFactory.class)
@Slf4j
public class Xia17RedisAutoconfiguration implements InitializingBean {

    /**
     * redisTemplate<String,Object> 实例
     * 使用jackson 序列化
     * @param redisConnectionFactory redis 链接工程
     * @return redis 模板
     */
    @Bean
    @ConditionalOnMissingBean(name = "redisStrTemplate")
    public RedisTemplate<String, Object> redisStrTemplate(RedisConnectionFactory redisConnectionFactory){
        return RedisTemplateFactory.redisStrTemplate(redisConnectionFactory,false);
    }

    /**
     * redisTemplate<Object,Object> 实例
     * 使用jackson 序列化
     * @param redisConnectionFactory redis 链接工厂
     * @return redis 模板
     */
    @Bean
    @ConditionalOnMissingBean(name = "redisObjTemplate")
    public RedisTemplate<Object, Object> redisObjTemplate(RedisConnectionFactory redisConnectionFactory){
        return RedisTemplateFactory.redisObjTemplate(redisConnectionFactory,false);
    }

    /**
     * 将 redisUtils 注入容器
     * @param redisTemplate /
     * @return /
     */
    @Bean
    @ConditionalOnMissingBean(name = "redisUtils")
    public RedisUtils redisUtils(RedisTemplate<String, Object> redisTemplate){
        return new RedisUtils(redisTemplate);
    }


    @Override
    public void afterPropertiesSet() {
        log.info("xia17fast-common: RedisUtils、RedisTemplate<Object, Object>、RedisTemplate<String, Object>已注入容器！");
    }
}
